
//json的定义
var person = {
    "firstname": "张",
    "lastname": "三丰",
    "age": 100,
    'hobby': ['打太极', '吃饭', '睡觉'],
    'goddess': {
        'firstname': '郭',
        'lastname': '襄',
        'age': 18
    }
};

//json的访问
// console.log(person.firstname);
// console.log(person.lastname);
// console.log(person.age);
// console.log(person.hobby[0]);
// console.log(person.goddess.firstname);
// console.log(person.goddess.lastname);

// json数组
let hs = [{
    "firstname": "张",
    "lastname": "三丰",
    "age": 100
},
{
    "firstname": "张",
    "lastname": "翠山",
    "age": 58
},
{
    "firstname": "张",
    "lastname": "无忌",
    "age": 23
}
];

// console.log(hs[0].firstname);

// //json的遍历
// for (let i = 0; i < hs.length; i++) {
//     console.log(hs[i].firstname);
// }


let wxb = {
    "name": "韦小宝",
    "age": 18,
    "addr": "北京",
    "wife": [{
        "name": "小双",
        "age": 18,
        "addr": "扬州"
    },
    {
        "name": "建宁",
        "age": 18,
        "addr": "北京"
    },
    {
        "name": "龙儿",
        "age": 38,
        "addr": "岛国"
    },
    {
        "name": "阿珂",
        "age": 17,
        "addr": "中国台湾"
    }
    ]
};

// for (let i = 0; i < wxb.wife.length; i++) {
//     console.log(wxb.wife[i].name);
// }
// for (let item of wxb.wife) {
//     console.log(item.name);
// }

let str = '{"name":"韦小宝","age":18,"addr":"北京","wife":[{"name":"小双","age":18,"addr":"扬州"},{"name":"建宁","age":18,"addr":"北京"},{"name":"龙儿","age":38,"addr":"岛国"},{"name":"阿珂","age":17,"addr":"中国台湾"}]}';

// string --> json
let json = JSON.parse(str);
console.log(typeof json);
console.log(json.name);
console.log(json);

// json --> string
let str2 = JSON.stringify(json);
console.log(typeof str2);
console.log(str2);